
/* ************************************************************************************* 
 * CREATED BY:          MYGENERATION
 * CREATED DATE:        12/25/2008
 * PROJECT:             MCWEBSITE
 * PURPOSE:             websiteNewsDAL class
 * MODIFIED BY:         None
 * MODIFIED DATE:       None
 * MODIFIED REASON:     None
 * NOTES:               None
 **************************************************************************************/

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Collections;
using System.Configuration;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;
using System.Web.Configuration;
using MCWEBSITE.ENTITY;

namespace MCWEBSITE.DAL
{
    public class websiteNewsDAL
    {
        #region  Properties

        public static string FIELD_NEWSID = "NewsID";
        public static string FIELD_LANGUAGECODE = "LanguageCode";
        public static string FIELD_CATEGORYID = "CategoryID";
        public static string FIELD_NEWSCODE = "NewsCode";
        public static string FIELD_TITTLE = "Tittle";
        public static string FIELD_SUMMARY = "Summary";
        public static string FIELD_NEWSCONTENT = "NewsContent";
        public static string FIELD_DESCRIPTION = "Description";
        public static string FIELD_CREATER = "Creater";
        public static string FIELD_CREATDATE = "CreatDate";
        public static string FIELD_IMAGE = "Image";
        public static string FIELD_ISACTIVE = "IsActive";
        public static string FIELD_ATRIBUTE1 = "Atribute1";
        public static string FIELD_ATRIBUTE2 = "Atribute2";
        public static string FIELD_ATRIBUTE3 = "Atribute3";
        public static string ConnectionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["ContentConnectionString"].ToString();

        #endregion

        #region Protected methods

        /// <summary>
        /// Select entity of websiteNews
        /// </summary>
        /// <param name="NewsID">Primary Keys</param>
        /// <returns>Collection of websiteNewsInfo</returns>
        public static websiteNewsInfo SelectOne(int NewsID)
        {
            websiteNewsInfo _websiteNewsInfo = new websiteNewsInfo();
            SqlDataReader dr = null;
            try
            {
                dr = SqlHelper.ExecuteReader(ConnectionString, "sp_mc_websiteNewsSelectOne", NewsID);
                if (dr.Read())
                    setwebsiteNewsInfo(dr, ref _websiteNewsInfo);
                return _websiteNewsInfo;
            }
            catch (Exception ex)
            {
                throw;
            }
            finally
            {
                dr.Close();
            }
        }

        /// <summary>
        /// Select all websiteNews
        /// </summary>
        /// <returns>Collection of websiteNewsInfo</returns>
        public static List<websiteNewsInfo> SelectAll()
        {
            List<websiteNewsInfo> lstwebsiteNews = new List<websiteNewsInfo>();
            SqlDataReader dr = null;
            try
            {
                dr = SqlHelper.ExecuteReader(ConnectionString, "sp_mc_websiteNewsSelectAll");
                setListwebsiteNewsInfo(ref dr, ref lstwebsiteNews);
                return lstwebsiteNews;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dr.Close();
            }
        }

        public static List<websiteNewsInfo> SelectByLanguage(string LanguageCode)
        {
            List<websiteNewsInfo> lstwebsiteNews = new List<websiteNewsInfo>();
            SqlDataReader dr = null;
            try
            {
                dr = SqlHelper.ExecuteReader(ConnectionString, "sp_mc_websiteNewsSelectByLanguage", LanguageCode);
                setListwebsiteNewsInfo(ref dr, ref lstwebsiteNews);
                return lstwebsiteNews;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dr.Close();
            }
        }
        //

        public static DataSet SelectNtoMByLanguage(int N, int M, string LanguageCode)
        {
            try
            {
                DataSet ds = new DataSet();
                ds = SqlHelper.ExecuteDataset(ConnectionString, "sp_mc_websiteNewsSelectNtoMByLanguage", N, M, LanguageCode);
                return ds;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public static List<websiteNewsInfo> SelectNtoMByLanguageByCat(int N, int M, string LanguageCode, int CatID)
        {
            List<websiteNewsInfo> lstwebsiteNews = new List<websiteNewsInfo>();
            SqlDataReader dr = null;
            try
            {
                dr = SqlHelper.ExecuteReader(ConnectionString, "sp_mc_websiteNewsSelectNtoMByLanguageByCatID", N, M, LanguageCode, CatID);
                setListwebsiteNewsInfo(ref dr, ref lstwebsiteNews);
                return lstwebsiteNews;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dr.Close();
            }
        }

        public static DataSet SelectNtoMByLanguageByCatDS(int N, int M, string LanguageCode, int CatID)
        {
            DataSet ds = new DataSet();
            try
            {
                ds = SqlHelper.ExecuteDataset(ConnectionString, "sp_mc_websiteNewsSelectNtoMByLanguageByCatID", N, M, LanguageCode, CatID);
                return ds;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static DataSet SelectNtoMByLanguageByCatByNewsIdDS(int N, int M, string LanguageCode, int CatID, int NewsId)
        {
            DataSet ds = new DataSet();
            try
            {
                ds = SqlHelper.ExecuteDataset(ConnectionString, "sp_mc_websiteNewsSelectNtoMByLanguageByCatByNewsID", N, M, LanguageCode, CatID, NewsId);
                return ds;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static DataSet getByCat(string LanguageCode, int CatID)
        {
            DataSet ds = new DataSet();
            try
            {
                ds = SqlHelper.ExecuteDataset(ConnectionString, "sp_mc_websiteNewsSelectByLanguageByCatID", LanguageCode, CatID);
                return ds;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static DataSet GetByLanguageByNewsID(string LanguageCode, int NewsID)
        {
            DataSet ds = new DataSet();
            try
            {
                ds = SqlHelper.ExecuteDataset(ConnectionString, "sp_mc_websiteNewsSelectByLanguageByNewsID", LanguageCode, NewsID);
                return ds;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        /// <summary>
        /// Select
        /// </summary>
        /// <param name="websiteNewsnfo">Modify this method adapting your need</param>
        /// <returns>Collection of websiteNewsInfo</returns>
        public static List<websiteNewsInfo> SelectFilters()
        {
            string criterias = string.Empty;
            List<websiteNewsInfo> lstwebsiteNews = new List<websiteNewsInfo>();
            SqlDataReader dr = null;
            try
            {
                dr = SqlHelper.ExecuteReader(ConnectionString, "sp_mc_websiteNewsSelectByFilters");
                setListwebsiteNewsInfo(ref dr, ref lstwebsiteNews);
                return lstwebsiteNews;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dr.Close();
            }
        }

        /// <summary>
        /// Insert websiteNews
        /// </summary>
        /// <param name="websiteNewsInfo">Entity of websiteNews</param>
        /// <returns>error code</returns>
        public static int Insert(websiteNewsInfo _websiteNewsInfo)
        {
            try
            {
                int NewsId = 0;
                NewsId = Convert.ToInt32(SqlHelper.ExecuteScalar(ConnectionString, "sp_mc_websiteNewsInsert"
                                             , _websiteNewsInfo.LanguageCode
                                             , _websiteNewsInfo.CategoryID
                                             , _websiteNewsInfo.Tittle
                                             , _websiteNewsInfo.Summary
                                             , _websiteNewsInfo.NewsContent
                                             , _websiteNewsInfo.Creater
                                             , _websiteNewsInfo.CreatDate
                                             , _websiteNewsInfo.IsActive
                                             ));

                return NewsId;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        /// <summary>
        /// Update websiteNews
        /// </summary>
        /// <param name="websiteNewsInfo">Entity of websiteNews</param>
        /// <returns>error code</returns>
        public static int Update(websiteNewsInfo _websiteNewsInfo)
        {
            try
            {
                SqlHelper.ExecuteNonQuery(ConnectionString, "sp_mc_websiteNewsUpdate"
                                            , _websiteNewsInfo.NewsID
                                            , _websiteNewsInfo.LanguageCode
                                            , _websiteNewsInfo.CategoryID
                                            , _websiteNewsInfo.NewsCode
                                            , _websiteNewsInfo.Tittle
                                            , _websiteNewsInfo.Summary
                                            , _websiteNewsInfo.NewsContent
                                            , _websiteNewsInfo.Description
                                            , _websiteNewsInfo.Creater
                                            , _websiteNewsInfo.CreatDate
                                            , _websiteNewsInfo.Image
                                            , _websiteNewsInfo.IsActive
                                            , _websiteNewsInfo.Atribute1
                                            , _websiteNewsInfo.Atribute2
                                            , _websiteNewsInfo.Atribute3
                                            );
                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }


        /// <summary>
        /// Delete SchoolAdmin
        /// </summary>
        /// <param name="NewsID">Primary Key</param>
        /// <returns>error code</returns>
        public static int Delete(int NewsID)
        {
            try
            {
                SqlHelper.ExecuteNonQuery(ConnectionString, "sp_mc_websiteNewsDelete", NewsID);
                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        public static int InsertResource(int NewsID, string Resources)
        {
            try
            {
                SqlHelper.ExecuteNonQuery(ConnectionString, "sp_mc_websiteNewsInsertResource", NewsID, Resources);
                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        public static int publish(int NewsId)
        {
            try
            {
                SqlHelper.ExecuteNonQuery(ConnectionString, "sp_mc_websiteNewspublish", NewsId);
                return 1;
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("Error:{0}", ex.Message));
            }
        }

        public static int unpublish(int NewsId)
        {
            try
            {
                SqlHelper.ExecuteNonQuery(ConnectionString, "sp_mc_websiteNewsunpublish", NewsId);
                return 1;
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("Error:{0}", ex.Message));
            }
        }

        public static int SaveComposeResource(websiteNewsInfo objInfo)
        {
            try
            {
                SqlHelper.ExecuteNonQuery(ConnectionString, "sp_mc_websiteNews_UpdateResourceContent"
                                            , objInfo.NewsID
                                            , objInfo.NewsContent
                                            );
                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        public static DataSet GetNewsWithFilter(string NewsId, string Title, string CreateDate, string status)
        {
            try
            {
                DataSet ds = new DataSet();
                ds = SqlHelper.ExecuteDataset(ConnectionString, "sp_mc_websiteNewsGetWithFilter", "'" + NewsId + "'", "N'%" + Title + "%'", "'" + CreateDate + "'", "'" + status + "'");
                return ds;
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("Error:{0}", ex.Message));
            }
        }

        public static DataSet GetNewsWithFilterLanguage(string NewsId, string Title, string CreateDate, string status, string strLanguage)
        {
            try
            {
                DataSet ds = new DataSet();
                ds = SqlHelper.ExecuteDataset(ConnectionString, "sp_mc_websiteNewsGetWithFilterLanguage", "'" + NewsId + "'", "N'%" + Title + "%'", "'" + CreateDate + "'", "'" + status + "'", "'" + strLanguage + "'");
                return ds;
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("Error:{0}", ex.Message));
            }
        }

        #endregion

        #region "Utilities method"

        private static void setListwebsiteNewsInfo(ref SqlDataReader dr, ref List<websiteNewsInfo> lstwebsiteNews)
        {
            websiteNewsInfo _websiteNewsInfo = null;
            while (dr.Read())
            {
                _websiteNewsInfo = new websiteNewsInfo();
                setwebsiteNewsInfo(dr, ref _websiteNewsInfo);
                lstwebsiteNews.Add(_websiteNewsInfo);
            }
        }

        private static void setwebsiteNewsInfo(SqlDataReader dr, ref websiteNewsInfo _websiteNewsInfo)
        {
            _websiteNewsInfo.NewsID = dr[websiteNewsDAL.FIELD_NEWSID] != DBNull.Value ? Convert.ToInt32(dr[websiteNewsDAL.FIELD_NEWSID]) : 0;
            _websiteNewsInfo.LanguageCode = dr[websiteNewsDAL.FIELD_LANGUAGECODE] != DBNull.Value ? Convert.ToString(dr[websiteNewsDAL.FIELD_LANGUAGECODE]) : string.Empty;
            _websiteNewsInfo.CategoryID = dr[websiteNewsDAL.FIELD_CATEGORYID] != DBNull.Value ? Convert.ToInt32(dr[websiteNewsDAL.FIELD_CATEGORYID]) : 0;
            _websiteNewsInfo.NewsCode = dr[websiteNewsDAL.FIELD_NEWSCODE] != DBNull.Value ? Convert.ToString(dr[websiteNewsDAL.FIELD_NEWSCODE]) : string.Empty;
            _websiteNewsInfo.Tittle = dr[websiteNewsDAL.FIELD_TITTLE] != DBNull.Value ? Convert.ToString(dr[websiteNewsDAL.FIELD_TITTLE]) : string.Empty;
            _websiteNewsInfo.Summary = dr[websiteNewsDAL.FIELD_SUMMARY] != DBNull.Value ? Convert.ToString(dr[websiteNewsDAL.FIELD_SUMMARY]) : string.Empty;
            _websiteNewsInfo.NewsContent = dr[websiteNewsDAL.FIELD_NEWSCONTENT] != DBNull.Value ? Convert.ToString(dr[websiteNewsDAL.FIELD_NEWSCONTENT]) : null;
            _websiteNewsInfo.Description = dr[websiteNewsDAL.FIELD_DESCRIPTION] != DBNull.Value ? Convert.ToString(dr[websiteNewsDAL.FIELD_DESCRIPTION]) : string.Empty;
            _websiteNewsInfo.Creater = dr[websiteNewsDAL.FIELD_CREATER] != DBNull.Value ? Convert.ToString(dr[websiteNewsDAL.FIELD_CREATER]) : string.Empty;
            _websiteNewsInfo.CreatDate = dr[websiteNewsDAL.FIELD_CREATDATE] != DBNull.Value ? Convert.ToDateTime(dr[websiteNewsDAL.FIELD_CREATDATE]) : DateTime.Now;
            _websiteNewsInfo.Image = dr[websiteNewsDAL.FIELD_IMAGE] != DBNull.Value ? Convert.ToString(dr[websiteNewsDAL.FIELD_IMAGE]) : string.Empty;
            _websiteNewsInfo.IsActive = dr[websiteNewsDAL.FIELD_ISACTIVE] != DBNull.Value ? Convert.ToBoolean(dr[websiteNewsDAL.FIELD_ISACTIVE]) : false;
            _websiteNewsInfo.Atribute1 = dr[websiteNewsDAL.FIELD_ATRIBUTE1] != DBNull.Value ? Convert.ToString(dr[websiteNewsDAL.FIELD_ATRIBUTE1]) : string.Empty;
            _websiteNewsInfo.Atribute2 = dr[websiteNewsDAL.FIELD_ATRIBUTE2] != DBNull.Value ? Convert.ToString(dr[websiteNewsDAL.FIELD_ATRIBUTE2]) : string.Empty;
            _websiteNewsInfo.Atribute3 = dr[websiteNewsDAL.FIELD_ATRIBUTE3] != DBNull.Value ? Convert.ToString(dr[websiteNewsDAL.FIELD_ATRIBUTE3]) : string.Empty;

        }
        #endregion Methods

        #region Private Members

        private static string TABLE_NAME = "[mc_websiteNews]";

        #endregion
    }
}

